/*     */ package com.zimbra.cs.dav.caldav;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.Instance;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import java.util.GregorianCalendar;
/*     */ import java.util.TimeZone;
/*     */ import org.dom4j.Element;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class Range
/*     */ {
/*     */   protected long mStart;
/*     */   protected long mEnd;
/*     */   
/*     */   public Range(String name)
/*     */   {
/*     */     try
/*     */     {
/*  45 */       Account acct = Provisioning.getInstance().get(Key.AccountBy.name, name);
/*  46 */       if (acct != null) {
/*  47 */         this.mStart = acct.getTimeInterval("zimbraCalendarCalDavSyncStart", 0L);
/*  48 */         this.mEnd = acct.getTimeInterval("zimbraCalendarCalDavSyncEnd", 0L);
/*     */       }
/*     */     }
/*     */     catch (ServiceException se) {}
/*  52 */     long now = System.currentTimeMillis();
/*  53 */     if (this.mStart == 0L) {
/*  54 */       this.mStart = Long.MIN_VALUE;
/*     */     } else
/*  56 */       this.mStart = (now - this.mStart);
/*  57 */     if (this.mEnd == 0L) {
/*  58 */       this.mEnd = Long.MAX_VALUE;
/*     */     } else
/*  60 */       this.mEnd = (now + this.mEnd);
/*     */   }
/*     */   
/*     */   public Range(Element elem) {
/*  64 */     this.mStart = (this.mEnd = 0L);
/*     */     
/*  66 */     String s = elem.attributeValue("start");
/*  67 */     if (s != null) {
/*  68 */       this.mStart = parseDateWithUTCTime(s);
/*     */     }
/*  70 */     s = elem.attributeValue("end");
/*  71 */     if (s != null) {
/*  72 */       this.mEnd = parseDateWithUTCTime(s);
/*     */     }
/*  74 */     if (this.mStart == 0L)
/*  75 */       this.mStart = Long.MIN_VALUE;
/*  76 */     if (this.mEnd == 0L)
/*  77 */       this.mEnd = Long.MAX_VALUE;
/*     */   }
/*     */   
/*     */   private static long parseDateWithUTCTime(String time) {
/*  81 */     if ((time.length() != 8) && (time.length() != 16))
/*  82 */       return 0L;
/*  83 */     if (!time.endsWith("Z"))
/*  84 */       return 0L;
/*  85 */     TimeZone tz = TimeZone.getTimeZone("GMT");
/*     */     
/*  87 */     int index = 0;
/*  88 */     int year = Integer.parseInt(time.substring(index, index + 4));index += 4;
/*  89 */     int month = Integer.parseInt(time.substring(index, index + 2)) - 1;index += 2;
/*  90 */     int date = Integer.parseInt(time.substring(index, index + 2));index += 2;
/*  91 */     int sec; int min; int hour = min = sec = 0;
/*  92 */     if (time.length() == 16) {
/*  93 */       if (time.charAt(index) == 'T') index++;
/*  94 */       hour = Integer.parseInt(time.substring(index, index + 2));index += 2;
/*  95 */       min = Integer.parseInt(time.substring(index, index + 2));index += 2;
/*  96 */       sec = Integer.parseInt(time.substring(index, index + 2));index += 2;
/*     */     }
/*  98 */     GregorianCalendar calendar = new GregorianCalendar(tz);
/*  99 */     calendar.clear();
/* 100 */     calendar.set(year, month, date, hour, min, sec);
/* 101 */     return calendar.getTimeInMillis();
/*     */   }
/*     */   
/*     */   public void intersection(Range another) {
/* 105 */     if (this.mStart < another.mStart)
/* 106 */       this.mStart = another.mStart;
/* 107 */     if (this.mEnd > another.mEnd)
/* 108 */       this.mEnd = another.mEnd;
/*     */   }
/*     */   
/*     */   public long getStart() {
/* 112 */     return this.mStart;
/*     */   }
/*     */   
/*     */   public long getEnd() {
/* 116 */     return this.mEnd;
/*     */   }
/*     */   
/*     */   public static class TimeRange extends Range
/*     */   {
/*     */     public TimeRange(String name) {
/* 122 */       super();
/*     */     }
/*     */     
/*     */     public TimeRange(Element elem) {
/* 126 */       super();
/*     */     }
/*     */     
/*     */     public boolean matches(int mboxId, int itemId, long apptRangeStart, long apptRangeEnd)
/*     */     {
/* 131 */       if ((apptRangeStart >= this.mStart) && (apptRangeEnd <= this.mEnd))
/* 132 */         return true;
/* 133 */       if ((mboxId == 0) || (itemId == 0)) {
/* 134 */         return true;
/*     */       }
/*     */       try {
/* 137 */         Mailbox mbox = MailboxManager.getInstance().getMailboxById(mboxId);
/* 138 */         CalendarItem item = mbox.getCalendarItemById(new OperationContext(mbox), itemId);
/* 139 */         for (CalendarItem.Instance instance : item.expandInstances(this.mStart, this.mEnd, false)) {
/* 140 */           if ((instance.getStart() < this.mEnd) && (instance.getEnd() > this.mStart)) {
/* 141 */             return true;
/*     */           }
/*     */         }
/*     */       } catch (ServiceException se) {
/* 145 */         ZimbraLog.dav.warn("error getting calendar item " + itemId + " from mailbox " + mboxId, se);
/*     */       }
/* 147 */       return false;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ExpandRange extends Range
/*     */   {
/*     */     public ExpandRange(Element elem) {
/* 154 */       super();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/caldav/Range.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */